package com.s7.statusbar.compat;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.s7.statusbar.StatusBarCompat;
import com.s7.statusbar.StatusBarManager;
import com.s7.statusbar.ColorManager;
import com.s7.statusbar.impl.StatusBar;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;

/**
 * 兼容LOLLIPOP版本
 */
public class LollipopCompat implements StatusBar {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setStatusBarColor(Window window, int color) {
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(color);
        StatusBarManager.internalSetFitsSystemWindows(window, true);
        StatusBarManager.resetActionBarContainerTopMargin(window);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void translucentStatusBar(Window window, boolean translucent) {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        if (translucent) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
        StatusBarManager.internalSetFitsSystemWindows(window, false);
        StatusBarManager.resetActionBarContainerTopMargin(window);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setToolbarColor(Window window, View toolbar, int color, int alpha) {
        StatusBarManager.setToolbar(window, toolbar);
        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, alpha));
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setCollapsingToolbar(final Window window, AppBarLayout bar, final CollapsingToolbarLayout collapsing,
                                     final Toolbar toolbar, final int color) {
        ((View) bar.getParent()).setFitsSystemWindows(false);
        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
        bar.setFitsSystemWindows(false);
        toolbar.setFitsSystemWindows(false);
        StatusBarManager.setToolbar(window, toolbar);
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) bar.getLayoutParams()).getBehavior();
        if (behavior != null && behavior instanceof AppBarLayout.Behavior) {
            int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset();
            if (Math.abs(verticalOffset) > collapsing.getScrimVisibleHeightTrigger()) {
                toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 1f));
            } else {
                toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
            }
        } else {
            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
        }
        collapsing.setFitsSystemWindows(false);
        final WeakReference<Window> weakReference = new WeakReference<>(window);
        bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout barLayout, int verticalOffset) {
                Window weakWindow = weakReference.get();
                if (weakWindow != null) {
                    int height = barLayout.getHeight() - collapsing.getScrimVisibleHeightTrigger();
                    float transparent = new BigDecimal(Math.abs(verticalOffset))
                    .divide(new BigDecimal(height), 2, BigDecimal.ROUND_HALF_UP).floatValue();
                    if (Math.abs(verticalOffset) >= height) {
                        toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 1f));
                    } else {
                        if (Math.abs(verticalOffset) > 0) {
                            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, transparent));
                        } else {
                            toolbar.setBackgroundColor(ColorManager.toColorAlpha(color, 0f));
                        }
                    }
                }
            }
        });
        collapsing.getChildAt(0).setFitsSystemWindows(false);
        collapsing.setStatusBarScrimColor(color);
    }
}
